<?php

/*
 * Overload of php function
 * Checks datetime from database
 */

/**
 * Description of Date
 *
  */
class Date {

    private static $day;
    private static $month;
    private static $year;
    private static $hour;
    private static $minute;
    private static $second;

    //datetime
    final private static function parse($data) {
        self::$year = substr($data, 0, 4);
        //echo self::$year . '<br />';
        //echo $data . '<br />';

        self::$month = substr($data, 5, 2);
        //echo self::$month . '<br />';

        self::$day = substr($data, 8, 2);
        //echo self::$day . '<br />';

        self::$hour = substr($data, 11, 2);
        //echo self::$hour . '<br />';

        self::$minute = substr($data, 14, 2);
        //echo self::$minute . '<br />';

        self::$second = substr($data, 17, 2);
        //echo self::$second . '<br />';
    }

    final public static function checkdate($data) {
        self::parse($data);

        return ( 0 <= self::$hour && self::$hour <= 23
        &&
        0 <= self::$minute && self::$minute <= 59
        &&
        0 <= self::$second && self::$second <= 59
        &&
        checkdate(self::$month, self::$day, self::$year));
    }

    final public static function toString() {
        self::parse($data);
        return "'" . self::$year . "-" . self::$month . "-" . self::$day . ' ' . self::$hour . ':' . self::$minute . ':' . self::$second . "'";
    }

}

function date_dbformat($date) {

    $date = preg_replace('^/[0-9]/-^', '', str_replace('/', '-', $date));
    if ($date == '') {
        $date = '0000-00-00';
    }
    $p_date = preg_split('/-/', $date);
    if (count($p_date) != 3 || checkdate($p_date[1], $p_date[2], $p_date[0]) == false && checkdate($p_date[1], $p_date[0], $p_date[2]) == false) {
        $date = '0000-00-00';
    } elseif (checkdate($p_date[1], $p_date[2], $p_date[0]) == true) {
        $date = $p_date[0] . '-' . $p_date[1] . '-' . $p_date[2];
    } elseif (checkdate($p_date[1], $p_date[2], $p_date[0]) == false && checkdate($p_date[1], $p_date[0], $p_date[2]) == true) {
        $date = $p_date[2] . '-' . $p_date[1] . '-' . $p_date[0];
    }
    return $date;
}